package com.msb.dongbao.ums.service.impl;

import com.msb.dongbao.common.base.enums.StateCodeEnum;
import com.msb.dongbao.common.base.result.ResultWrapper;
import com.msb.dongbao.entity.UmsMember;
import com.msb.dongbao.entity.UserMemberLoginResponse;
import com.msb.dongbao.entity.dto.UmsMemberLoginParamDTO;
import com.msb.dongbao.entity.dto.UmsMemberRegisterParamDTO;
import com.msb.dongbao.service.UmsMemberService;
import com.msb.dongbao.ums.mapper.UmsMemberMapper;
import com.msb.msbdongbaocommonutil.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * <p>
 * 后台用户表 服务实现类
 * </p>
 *
 * @author 李遥
 * @since 2020-12-23
 */
@Service
public class UmsMemberServiceImpl  implements UmsMemberService {

    @Autowired
    UmsMemberMapper umsMemberMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;


    public ResultWrapper register(UmsMemberRegisterParamDTO umsMemberRegisterParamDTO){
        UmsMember umsMember  = new UmsMember();
        BeanUtils.copyProperties(umsMemberRegisterParamDTO,umsMember);
        String encode = passwordEncoder.encode(umsMemberRegisterParamDTO.getPassword());
        umsMember.setPassword(encode);
        umsMemberMapper.insert(umsMember);
        return ResultWrapper.getSuccessBuilder().build();
    }


    public ResultWrapper login(UmsMemberLoginParamDTO umsMemberLoginParamDTO) {
        UmsMember umsMember = umsMemberMapper.selectByName(umsMemberLoginParamDTO.getUsername());
        if (null != umsMember) {
            String passwordDb = umsMember.getPassword();
            if (!passwordEncoder.matches(umsMemberLoginParamDTO.getPassword(), passwordDb)) {
                return ResultWrapper.getFAILBuilder().code(StateCodeEnum.PASSWORD_ERROR.getCode()).
                        msg(StateCodeEnum.PASSWORD_ERROR.getMsg()).build();
            }
        } else {
            return ResultWrapper.getFAILBuilder().code(StateCodeEnum.USER_EMPTY.getCode()).
                    msg(StateCodeEnum.USER_EMPTY.getMsg()).build();
        }
        String token = JwtUtil.createToken(umsMember.getId()+"");
        UserMemberLoginResponse userMemberLoginResponse = new UserMemberLoginResponse();
        userMemberLoginResponse.setToken(token);
        umsMember.setPassword("");
        userMemberLoginResponse.setUmsMember(umsMember);
        return ResultWrapper.getSuccessBuilder().data(userMemberLoginResponse).build();
    }

    @Override
    public ResultWrapper edit(UmsMember umsMember) {
        umsMemberMapper.updateById(umsMember);
        return ResultWrapper.getSuccessBuilder().data(umsMember).build();
    }
}
